package com.belazy.library.core.properties;


import com.belazy.library.util.constant.StringConstant;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.EnvironmentCapable;
import org.springframework.lang.Nullable;

import java.util.Arrays;
import java.util.List;

/**
 * @Description <p> 运行环境属性类  </p>
 * @Author chupeng.tang
 * @CreateTime 2023/5/19 17:29
 * @Version 1.0
 */
@Getter
@Setter
@Configuration
public class EnvironmentProperties implements EnvironmentAware, EnvironmentCapable {
	private static final String APP_ACTIVE = "spring.profiles.active";
	private static final String APP_NAME = "spring.application.name";
	@Nullable
	private Environment environment;

	/**
	 * <p>环境，方便在代码中获取</p>
	 * @auhor chupeng.tang
	 * @datetime 2023/5/19 17:55
	 * @return {@link String} 当前运行环境，默认返回 dev环境
	 **/
	public String getEnv() {
		String[] activeProfiles = environment.getActiveProfiles();
		List<String> activeProfileList = Arrays.asList(activeProfiles);
		if (activeProfileList.isEmpty()) {
			return environment.getProperty(APP_ACTIVE, "dev");
		} else {
			return activeProfileList.get(0);
		}
	}

	/**
	 * <p>应用名称${spring.application.name}</p>
	 * @auhor chupeng.tang
	 * @datetime 2023/5/19 17:55
	 * @return {@link String} 应用服务名称
	 **/
	public String getName() {
		return environment.getProperty(APP_NAME, StringConstant.EMPTY);
	}

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

	@Override
	public Environment getEnvironment() {
		return this.environment;
	}
}
